package com.example.clock.planPage;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.example.clock.R;
import com.example.clock.basicClass.Plan;
import com.example.clock.controlClass.DatabaseHelper;
import com.example.clock.controlClass.SqlController;
import com.example.clock.lockedPage.MainActivity;

import java.util.ArrayList;

public class PlanPage extends AppCompatActivity {
    ListView listView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }
    @Override
    protected void onStart() {
        setContentView(R.layout.activity_plan_page);
        listView=(ListView) findViewById(R.id.listview1);

        //获取所有plan
        DatabaseHelper dbhelper = new DatabaseHelper(this,this);
        SQLiteDatabase db = dbhelper.getReadableDatabase();
        ArrayList<Plan> planArrayList = SqlController.getAllPlans(db);

        planArrayList.add(new Plan("点击新建计划"));




        //构建适配器
        PlanAdapter adapter=new PlanAdapter(PlanPage.this,R.layout.list_item_1,planArrayList);
        listView.setAdapter(adapter);

        //设置点击事件
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // 处理点击事件，比如跳转到详细页面或者弹出对话框等
                Plan p = (Plan)parent.getItemAtPosition(position);
                if(p.getPlanName() == "点击新建计划"){
                    Intent intent = new Intent(PlanPage.this,NewPlan.class);
                    startActivity(intent);
                }else{
                    Intent intent = new Intent(PlanPage.this,DetailPlan.class);
                    intent.putExtra("planid",p.getPlanId());
                    startActivity(intent);
                }
            }
        });


        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener(){
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id){
                //处理长按事件
                Plan planNow = (Plan)listView.getItemAtPosition(position);
                String PlanId = planNow.getPlanId().toString();
                // 创建AlertDialog.Builder对象
                AlertDialog.Builder builder = new AlertDialog.Builder(PlanPage.this);
                builder.setTitle("删除计划");
                builder.setMessage("是否确定删除该计划？");

                // 设置PositiveButton（确定）按钮
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // 在这里执行删除操作
                        // 例如，调用删除计划的方法
                        SQLiteDatabase db = new DatabaseHelper(PlanPage.this,PlanPage.this).getWritableDatabase();
                        SqlController.deletePlan(db,PlanId);
                        PlanPage.this.onStart();
                    }
                });

                // 设置NegativeButton（取消）按钮
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // 取消删除操作
                        dialog.dismiss();
                    }
                });

                // 创建并显示对话框
                AlertDialog alertDialog = builder.create();
                alertDialog.show();

                return true;
            }
        });


        super.onStart();
    }
    public void getTodayPlan(View view){

        listView=(ListView) findViewById(R.id.listview1);

        //获取所有plan
        DatabaseHelper dbhelper = new DatabaseHelper(this,this);
        SQLiteDatabase db = dbhelper.getReadableDatabase();
        ArrayList<Plan> planArrayList = SqlController.getTodayPlans(db);

        planArrayList.add(new Plan("点击新建计划"));


        //构建适配器
        PlanAdapter adapter=new PlanAdapter(PlanPage.this,R.layout.list_item_1,planArrayList);
        listView.setAdapter(adapter);

        //设置点击事件
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // 处理点击事件，比如跳转到详细页面或者弹出对话框等

                Plan p = (Plan)parent.getItemAtPosition(position);
                if(p.getPlanName() == "点击新建计划"){
                    Intent intent = new Intent(PlanPage.this,NewPlan.class);
                    startActivity(intent);
                }else{
                    Intent intent = new Intent(PlanPage.this,DetailPlan.class);
                    intent.putExtra("planid",p.getPlanId());
                    startActivity(intent);
                }

            }
        });

    }
    public void getAllPlan(View view){
        listView=(ListView) findViewById(R.id.listview1);

        //获取所有plan
        DatabaseHelper dbhelper = new DatabaseHelper(this,this);
        SQLiteDatabase db = dbhelper.getReadableDatabase();
        ArrayList<Plan> planArrayList = SqlController.getAllPlans(db);

        planArrayList.add(new Plan("点击新建计划"));

        //构建适配器
        PlanAdapter adapter=new PlanAdapter(PlanPage.this,R.layout.list_item_1,planArrayList);
        listView.setAdapter(adapter);

        //设置点击事件
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // 处理点击事件，比如跳转到详细页面或者弹出对话框等
                Plan p = (Plan)parent.getItemAtPosition(position);
                if(p.getPlanName() == "点击新建计划"){
                    Intent intent = new Intent(PlanPage.this,NewPlan.class);
                    startActivity(intent);
                }else{
                    Intent intent = new Intent(PlanPage.this,DetailPlan.class);
                    intent.putExtra("planid",p.getPlanId());
                    startActivity(intent);
                }
            }
        });

    }

}
